<template>
	<div>
		<div>
			<el-form :inline="true" :model="queryParams" class="demo-form-inline">
				<el-form-item label="姓名">
					<el-input v-model="queryParams.name" placeholder="姓名"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="queryAll()">查询</el-button>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="dialogFormVisible = true">添加</el-button>
				</el-form-item>
			</el-form>
			<!-- 新增模态框 -->
			<el-dialog title="新增" :visible.sync="dialogFormVisible">
				<el-form :model="TGoodsUnit">
					<el-form-item label="单位名称" :label-width="formLabelWidth">
						<el-input v-model="TGoodsUnit.name" autocomplete="off"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="dialogFormVisible = false">取 消</el-button>
					<el-button type="primary" @click="insertTGoodsUnit()">确 定</el-button>
				</div>
			</el-dialog>

		</div>
		<div>
			<el-table :data="tableData" style="width: 100%" height="250">
				<el-table-column fixed prop="id" label="单位编号" width="150" align="center">
				</el-table-column>
				<el-table-column prop="name" label="单位名称" width="120" align="center">
				</el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button size="mini" type="danger"
							@click="handleDelete(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>\
			<!-- 修改模态框 -->
			<el-dialog title="修改" :visible.sync="dialogFormVisible01">
				<el-form :model="TGoodsUnit">
					<el-form-item label="单位名称" :label-width="formLabelWidth">
						<el-input v-model="TGoodsUnit.name" autocomplete="off"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="dialogFormVisible01 = false">取 消</el-button>
					<el-button type="primary" @click="updateTGoodsUnit">确 定</el-button>
				</div>
			</el-dialog>
		</div>
		<div>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryParams.currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="queryParams.pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="queryNum">
			</el-pagination>
		</div>
	</div>
</template>

<script>
	import {
		putRequest
	} from '@/utils/api';
	export default {
		data() {
			return {
				dialogFormVisible01: false,
				//新增属性
				dialogFormVisible: false,
				TGoodsUnit: {
					id: null,
					name: ""
				},
				formLabelWidth: '120px',
				//分页属性
				queryNum: 20,
				queryParams: {
					name: "",
					pageSize: 5,
					currentPage: 1
				},
				tableData: [],
				admin:{
					name:""
				},
			}
		},
		mounted() {
			var admin = sessionStorage.getItem("admin");
			this.admin = admin;
			this.queryAll();
		},
		methods: {
			updateTGoodsUnit() {
				this.$postRequest("/tGoodsUnit/insertAndUpdateTGoodsUnit", this.TGoodsUnit).then(res => {
					this.dialogFormVisible01 = false;
					this.queryAll();
				})
			},
			insertTGoodsUnit() {
				this.$postRequest("/tGoodsUnit/insertAndUpdateTGoodsUnit", this.TGoodsUnit).then(res => {
					this.dialogFormVisible = false;
					this.queryAll();
				})
			},
			queryAll() {
				this.$postRequest("/tGoodsUnit/queryAll", this.queryParams).then(res => {
					this.tableData = res.data.tGoodsUnitList;
					this.queryNum = res.data.queryNum;
				})
			},
			handleEdit(index, row) {
				console.log(index, row);
				this.dialogFormVisible01 = true;
				this.TGoodsUnit = JSON.parse(JSON.stringify(row));
			},
			handleDelete(index, row) {
				console.log(row.id)
				this.$confirm('此操作将永久删除, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$deleteRequest("/tGoodsUnit/deleteTGoodsUnit/" + row.id).then(res => {
						this.queryAll();
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});

			},
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.queryParams.pageSize = val;
				this.queryAll();

			},
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.queryParams.currentPage = val;
				this.queryAll();
			}
		}
	}
</script>

<style>
</style>